﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FileOps
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            clearFileField();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            // Create named file
            int startPosition = txtFileName.Text.Length - 4;
            int endPosition = 4;
            string strFileName = txtFileName.Text.Trim();
            string strErrorMessage = "You must enter a valid file name. At least 1 character\nlong ending with file extension .txt";
            if (strFileName.Length < 5)
            {
                MessageBox.Show(strErrorMessage);
            }
            else
            {
                string strFileCheck = strFileName.Substring(startPosition, endPosition);
                if (strFileCheck != ".txt")
                {
                    MessageBox.Show(strErrorMessage);
                }
                else
                {
                    TextWriter tw = new StreamWriter(strFileName);
                    tw.Write(txtFileContents.Text);
                    tw.Close();
                    clearFileField();
                }
            }
        }

        private void clearFileField()
        {
            txtFileName.Text = string.Empty;
            txtFileContents.Text = string.Empty;
            txtFileName.Focus();
        }
    }
}
